package com.lp.controller;

import com.lp.bo.ProDictionaryInfoBO;
import com.lp.cache.CacheName;
import com.lp.cache.ProCache;
import com.lp.cfg.MinioConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/minio")
@Slf4j
public class MinioController extends BaseController{

    @Autowired
    MinioConfig minioConfig;

    @Autowired
    private ProCache procahce;

    @RequestMapping("/list")
    public List<String> list() throws Exception {
        System.out.println("minio list");
        return this.minioConfig.listBucketNames();
    }

    @RequestMapping(method = RequestMethod.POST, value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file, @RequestParam("id") Integer id) throws Exception {
        String fileUrl = this.minioConfig.putObject(file);
        ProDictionaryInfoBO proDictionaryInfoBO = new ProDictionaryInfoBO();
        proDictionaryInfoBO.setId(id);
        proDictionaryInfoBO.setUrl(fileUrl);
        Map<String, Object> resultMap = service.update("ProDictionaryInfo.update", proDictionaryInfoBO) ;
        if (isOk(resultMap)){
            procahce.refleshCache(CacheName.DICTIONARY);
            return "ok";
        }
        return "error";
    }

    // 下载文件
    @RequestMapping(method = RequestMethod.GET, value = "/download")
    public void download(@RequestParam("fileName")String fileName, HttpServletResponse response) {
        this.minioConfig.download(fileName,response);
    }

    // 创建存储桶
    @RequestMapping(method = RequestMethod.POST, value = "/createBucket")
    public boolean createBucket(String bucketName) throws Exception {
        return this.minioConfig.makeBucket(bucketName);
    }

    // 删除存储桶
    @RequestMapping(method = RequestMethod.POST, value = "/deleteBucket")
    public boolean deleteBucket(String bucketName) throws Exception {
        return this.minioConfig.removeBucket(bucketName);
    }

    // 列出存储桶中的所有对象名称
    @RequestMapping(method = RequestMethod.POST, value = "/listObjectNames")
    public List<String> listObjectNames(String bucketName) throws Exception {
        return this.minioConfig.listObjectNames(bucketName);
    }

    // 删除一个对象
    @RequestMapping(method = RequestMethod.POST, value = "/removeObject")
    public boolean removeObject(String bucketName, String objectName) throws Exception {
        return this.minioConfig.removeObject(bucketName, objectName);
    }

    // 文件访问路径
    @RequestMapping(method = RequestMethod.POST, value = "/getObjectUrl")
    public String getObjectUrl(String bucketName, String objectName) throws Exception {
        return this.minioConfig.getObjectUrl(bucketName, objectName);
    }
}
